package com.huluobo.lc.libnetwork;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Type;

/**
 * @author Lc
 * @description: 功能:实现json的转换,从一个response,到具体的对象(Bean),可通过type实现转换,可通过class类型转换
 * @date :2020/7/14 9:38
 */
public class JsonConvert implements Convert {
    @Override
    public Object convert(String response, Type type) {
        Object object = null;
        JSONObject jsonObject = JSON.parseObject(response);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data != null) {
            Object data1 = data.get("data");
            object = JSON.parseObject(data1.toString(), type);
        }
        return object;
    }

    @Override
    public Object convert(String response, Class claz) {
        Object object = null;
        JSONObject jsonObject = JSON.parseObject(response);
        JSONObject data = jsonObject.getJSONObject("data");
        if (data != null) {
            Object data1 = data.get("data");
            object = JSON.parseObject(data1.toString(), claz);
        }
        return object;
    }
}
